package com.app.couponsharelife.ui.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.databinding.DataBindingUtil
import com.bumptech.glide.Glide
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.LifeHomePageBean
import com.app.couponsharelife.databinding.LocalShopsTypeItemBinding
import com.app.couponsharelife.manage.Contacts
import com.app.couponsharelife.ui.listener.OnClickListener
import com.app.couponsharelife.utils.AppUtils

class LocalShopsTypeAdapter(private val list: List<LifeHomePageBean.ClassifyBean>,
                            private val listener: OnClickListener) :
        androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        return ViewHolder(DataBindingUtil.inflate(
                LayoutInflater.from(parent.context), R.layout.local_shops_type_item, parent, false))
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
        holder as ViewHolder
        Glide.with(holder.itemView.context).asBitmap()
                .load(Contacts.REMOTE_IMAGE_URL + list[position].picUrl)
                .into(holder.databinding.ivTypeImage)
        holder.databinding.tvSortType.text = list[position].name
    }

    inner class ViewHolder(val databinding: LocalShopsTypeItemBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(databinding.root) {
        init {
            val width = (AppUtils.getDisplayMetrics(databinding.root.context).widthPixels * 0.936 * 0.2).toInt()
            databinding.root.layoutParams = ConstraintLayout.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT)
            databinding.root.setOnClickListener {
                if (adapterPosition != -1) {
                    listener.click(0, list[adapterPosition])
                }
            }
        }
    }
}